package com.srini.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.srini.pojos.LoggedInUser;
import com.srini.pojos.PaidDayOff;
import com.srini.util.AppUtil;
import com.srini.util.ConnectionUtil;
import com.srini.util.Constants;
import com.vssr.persist.DomainObject;

/**
 * Servlet implementation class GetDataServlet
 */
public class GetDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public GetDataServlet() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		LoggedInUser user = (LoggedInUser) request.getSession().getAttribute(
				Constants.LOGGEDIN_USER);
		try {
			PaidDayOff off = null;
			JSONArray array = new JSONArray();
			JSONObject obj = new JSONObject();
			JSONArray cell = null;
			JSONObject cellObj = null;

			if (action == null) {
				String query = "select * from leaves where type='1' and finyear='"
						+ Calendar.getInstance().get(Calendar.YEAR) + "'";
				List dataList = DomainObject.getDBRecords(new PaidDayOff(),
						query, new ConnectionUtil().getConnection());

				for (int i = 0; i < dataList.size(); i++) {
					cell = new JSONArray();
					cellObj = new JSONObject();
					off = (PaidDayOff) dataList.get(i);
					cellObj.put("id", off.getId());
					cell.put(off.getPaidDayOff());
					cell.put(off.getDescription());
					cellObj.put("cell", cell);
					array.put(cellObj);
				}

				obj.put("rows", array);
				obj.put("page", Long.parseLong("1"));
				out.print(obj);

			} else if (action.equalsIgnoreCase("dataForH")) {
				String query = "select * from leaves where type='1' and finyear='"
						+ Calendar.getInstance().get(Calendar.YEAR) + "'";
				List dataList = DomainObject.getDBRecords(new PaidDayOff(),
						query, new ConnectionUtil().getConnection());

				for (int i = 0; i < dataList.size(); i++) {
					cell = new JSONArray();
					cellObj = new JSONObject();
					off = (PaidDayOff) dataList.get(i);
					cellObj.put("title", off.getDescription());
					cellObj.put("start", off.getPaidDayOff());
					array.put(cellObj);

				}
				out.print(array);
			} else if (action.equalsIgnoreCase("usersList")) {
				String query = "select * from usertable";
				List dataList = DomainObject.getDBRecords(new LoggedInUser(),
						query, new ConnectionUtil().getConnection());
				LoggedInUser userIn = null;
				for (int i = 0; i < dataList.size(); i++) {
					cell = new JSONArray();
					cellObj = new JSONObject();
					userIn = (LoggedInUser) dataList.get(i);
					cellObj.put("id", userIn.getId());
					cell.put(userIn.getId());
					cell.put(userIn.getUserName());
					cell.put(userIn.getUserInfo().getFirstName());
					cell.put(userIn.getUserInfo().getLastName());
					cellObj.put("cell", cell);
					array.put(cellObj);
				}

				obj.put("rows", array);
				obj.put("page", Long.parseLong("1"));
				out.print(obj);

			} else if (action.equalsIgnoreCase("managerList")) {
				String startsWith = request.getParameter("term");
				String query = "select * from usertable where upper(username) like upper('"
						+ startsWith + "%')";
				List dataList = DomainObject.getDBRecords(new LoggedInUser(),
						query, new ConnectionUtil().getConnection());
				LoggedInUser manager = null;
				String userName = "";
				for (int i = 0; i < dataList.size(); i++) {
					obj = new JSONObject();
					manager = (LoggedInUser) dataList.get(i);
					if (manager.getUserInfo() != null) {
						userName = manager.getUserInfo().getFullDisplayName();
					}
					if (AppUtil.isEmpty(userName)) {
						userName = manager.getUserName();
					}
					obj.put("label", userName);
					obj.put("id", manager.getId());
					array.put(obj);
				}
				out.print(array);
			} else if (action.equalsIgnoreCase("getReportees")) {

				String userId = request.getParameter("userId");
				int intUserId = userId == null ? user.getId() : Integer
						.parseInt(userId);
				String query = "select * from usertable where manager = "
						+ intUserId, userName = null;
				List dataList = DomainObject.getDBRecords(new LoggedInUser(),
						query, new ConnectionUtil().getConnection());
				LoggedInUser userIn = null;
				boolean isDisabled = false;
				String vInfo = null;
				for (int i = 0; i < dataList.size(); i++) {
					userIn = (LoggedInUser) dataList.get(i);
					userName = userIn.getUserInfo().getFullDisplayName();
					userName = AppUtil.isEmpty(userName) ? userIn.getUserName()
							: userName;
					isDisabled = false;
					obj = new JSONObject();
					obj.put("id", userIn.getId());
					obj.put("name", userName);
					vInfo = userIn.getUserInfo().getVehicle();
					/*
					 * if (vInfo != null && vInfo.equals("2")) {
					 * obj.put("disabled", "disabled"); obj.put("message",
					 * "Not eligible for reimbursement"); isDisabled = true; }
					 * else
					 */
					if (AppUtil.isEmpty(userIn.getUserInfo()
							.getDistanceFromHome())
							|| AppUtil.isEmpty(vInfo)
							&& !vInfo.equals("2")) {
						obj.put("disabled", "disabled");
						obj.put("message", "Profile is not yet activated");
						isDisabled = true;
					}
					if (!isDisabled) {
						obj.put("disabled", "");
						obj.put("message", "");
					}
					array.put(obj);
				}
				out.print(array);
			} else {

				String query = "select * from leaves where type='0'"
						+ " and userid=" + user.getId();
				List dataList = DomainObject.getDBRecords(new PaidDayOff(),
						query, new ConnectionUtil().getConnection());

				for (int i = 0; i < dataList.size(); i++) {
					obj = new JSONObject();
					off = (PaidDayOff) dataList.get(i);
					obj.put("id", off.getId());
					obj.put("date", off.getPaidDayOff());
					obj.put("state", "saved");
					array.put(obj);

				}
				out.print(array);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		out.close();
	}

}
